package com.yxw.webview01;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private WebView webView;

    private Button mRefresh;
    private Button mBack;

    private TextView titleView;

    private TextView textViewError;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = findViewById(R.id.webView);
        webView.loadUrl("http://shouji.baidu.com/");

        mRefresh = findViewById(R.id.refresh);
        mBack = findViewById(R.id.back);
        titleView = findViewById(R.id.title);
        textViewError = findViewById(R.id.textView_error);

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                titleView.setText(title);
                super.onReceivedTitle(view, title);
            }
        });

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
             //   view.loadUrl("file:///res/error.html");
                textViewError.setText("404 error");
                //webview本身有一个页面，要把它隐藏
                webView.setVisibility(View.GONE);
            }
        });
        webView.setDownloadListener(new MyDownload());


        mRefresh.setOnClickListener(new MyListener());
        mBack.setOnClickListener(new MyListener());

    }


    class MyListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.refresh:
                    webView.reload();
                    break;
                case R.id.back:
                    finish();
                    break;
                default:
                    break;
            }
        }
    }

    class MyDownload implements DownloadListener {
        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
            if (url.endsWith(".apk")) {

                new HttpThread(url).start();

                //调用系统的浏览器打开一个下载的目录
//                Uri uri = Uri.parse(url);
//                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//                startActivity(intent);
            }
        }
    }
}
